//
//  DetailShopViewController.m
//  L7_test
//
//  Created by Mac on 11-12-29.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "MoreOptionsController.h"
#import "UserFeedBackController.h"
#import "Constants.h"
#import "MoreManager.h"
#import "Version.h"
#import "FMDBTransactionHistory.h"
#import "FMDBCollectionManager.h"
#import "Statement.h"
#import "AboutOus.h"
#import "PublicFunction.h"
#import "iVersion.h"


@implementation MoreOptionsController
@synthesize firstSectionArray;
@synthesize secondSectionArray;

- (void)viewDidLoad {
    [super viewDidLoad];
    //用代码生成tableview并关联
    UITableView *moreTableView  = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStyleGrouped];
	moreTableView.dataSource = self;
	moreTableView.delegate = self;
	[self.view addSubview:moreTableView];


    //设置视图title
    self.title=@"更多";

    //修改导航栏背景
    NSString *filePath=[[NSBundle mainBundle] pathForResource:@"searchBarBg" ofType:@"png"];
    
<<<<<<< .mine
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithContentsOfFile:filePath] forBarMetrics: UIBarMetricsDefault];
    
    NSArray *array1 = [[NSArray alloc] initWithObjects:@"版本更新",@"清空缓存",@"我有意见",@"喜欢此应用，打分鼓励下",nil];
=======
    NSArray *array1 = [[NSArray alloc] initWithObjects:@"版本更新",@"清空缓存",@"我有意见",nil];
>>>>>>> .r261
    NSArray *array2 = [[NSArray alloc] initWithObjects:@"关于我们",@"免责声明", nil];
    firstSectionArray = array1;
    secondSectionArray = array2;




}

- (void)viewWillAppear:(BOOL)animated{
    //设置显示导航栏
    self.navigationController.navigationBarHidden=NO; 
}

//设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

//设置为2个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

//设置section的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger rows = 0;
    switch (section) {
        case 0:
            rows = [firstSectionArray count];
            break;
        case 1:
            rows = [secondSectionArray count];
            break;
        default:
            break;
    }
    return rows;
}

//设置每个cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //设置tableview的背景颜色为白色
    [tableView setBackgroundColor:[UIColor clearColor]];
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier:SimpleTableIdentifier];
        //设置cell的背景颜色为白色
        [cell setBackgroundColor:[UIColor clearColor]];
    }
    NSString *cellText = nil;
    switch (indexPath.section) {
        case 0:
            cellText = [firstSectionArray objectAtIndex:indexPath.row];
            break;
        case 1:
            cellText = [secondSectionArray objectAtIndex:indexPath.row];
            break;
        default:
            break;
    }
    cell.textLabel.text = cellText;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
    return cell;
}

-(void) checkUpdate{
    [[iVersion sharedInstance] checkForNewVersion];

}

//cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSURL *system_sound_url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1248" ofType:@"wav"]];
    SystemSoundID system_sound_id;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)system_sound_url, &system_sound_id);
    AudioServicesPlaySystemSound(system_sound_id);
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
//            [iVersion sharedInstance].lastReminded = nil;
//            [self performSelectorOnMainThread:@selector(checkUpdate) withObject:nil waitUntilDone:YES];
//            if (![[iVersion sharedInstance] shouldUpdate]){
//                CustomeAlertWithTitle(@"版本更新",@"您已经是最新版本啦!");
//            }
            NSString *str = [[NSString alloc]initWithFormat: 
                             @"https://itunes.apple.com/cn/app/dian-song/id560731876?mt=8", 
                             APP_ID ];  
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
        if(indexPath.row==1){
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"是否清除缓存" message:nil  delegate:self cancelButtonTitle:@"取  消" otherButtonTitles:@"确  定",nil];
            [alert show];
            ;
        }
        if (indexPath.row == 2) {
            [self showFeedBackView];
            
        }
<<<<<<< .mine
        if (indexPath.row == 3) {
//            NSString *str = [[NSString alloc]initWithFormat: 
//                             @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d", 
//                             APP_ID ];  
            NSString *str = [[NSString alloc]initWithFormat: 
                             @"https://itunes.apple.com/cn/app/dian-song/id560731876?mt=8", 
                             APP_ID ];  
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            
        }
=======
>>>>>>> .r261
    }
    if(indexPath.section==1){
        if(indexPath.row==0){//关于我们
            //创建新的试图并将试图推入栈中
            AboutOus *aboutOus = [[AboutOus alloc] init];
            [self.navigationController pushViewController: aboutOus animated:YES];
        }
        if(indexPath.row==1){//免责声明
            //创建新的试图并将试图推入栈中
            Statement *statement = [[Statement alloc] init];
            [self.navigationController pushViewController: statement animated:YES];

        }
    }
    //tableview的cell单元格取消高亮
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
}

//点击弹出确定按钮时
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {    
    if(buttonIndex==1){
        //清除最近浏览
        FMDBTransactionHistory *fmdbTransactionHistory=[[FMDBTransactionHistory alloc]init];
        [fmdbTransactionHistory deleteAllHistory];

        //清除收藏
        FMDBCollectionManager *fmdbCollectionManager=[[FMDBCollectionManager alloc]init];
        [fmdbCollectionManager deleteAllCollection];

        //清除图片
        [PublicFunction RemoveAllImages];
    }
}  

-(void)showFeedBackView {
    //创建新的试图并将试图推入栈中
    UserFeedBackController *userFeedBackController = [[UserFeedBackController alloc] init];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity :2];
    [dictionary setObject:@"F" forKey:@"type"];
    [dictionary setObject:@"我有意见" forKey:@"title"];
    [userFeedBackController setRecieveDictionary:dictionary];
    [self.navigationController pushViewController: userFeedBackController animated:YES];
}

//-(IBAction)showShopMenuAction:(id)sender {
//    //创建新的试图并将试图推入栈中
//    UserFeedBackController *userFeedBackController = [[UserFeedBackController alloc]initWithNibName:@"UserFeedBackController" bundle:nil];
//    UINavigationController  *theNavController = [[UINavigationController alloc]
//                                                 initWithRootViewController:userFeedBackController];
//    [userFeedBackController release];
//    theNavController.navigationBar.tintColor = [UIColor blackColor];
//    [self.navigationController pushViewController: userFeedBackController animated:YES];
//    [theNavController release];
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	self.firstSectionArray = nil;
    self.secondSectionArray = nil;
    [super viewDidUnload];
}

//- (void)dealloc {
//
//
//}
@end
